package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;


import java.net.Proxy;

	public class model_test {
	    public static void main(String[] args) {
	        String apiKey = "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";
	        String apiUrl =  "https://api.openai.com/v1/chat/completions";
	        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1",7890));
	        try {
	            URL url = new URL(apiUrl);
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
	            connection.setRequestMethod("POST");
	            connection.setRequestProperty("Authorization", "Bearer " + apiKey);
	            connection.setRequestProperty("Content-Type", "application/json");
	            connection.setDoOutput(true);

	            String jsonInput = "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"好吧！你还记得我上一句话讲了什么吗\"}]}";
	            
	            OutputStream outputStream = connection.getOutputStream();
	            outputStream.write(jsonInput.getBytes());
	            outputStream.flush();

	            // 设置请求参数和数据
	            // ...

	            // 发送请求
	            int responseCode = connection.getResponseCode();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            String line;
	            StringBuilder response = new StringBuilder();

	            while ((line = reader.readLine()) != null) {
	                response.append(line);
	            }
	            reader.close();

	            // 处理响应
	            if (responseCode == HttpURLConnection.HTTP_OK) {
	                // 解析响应数据
	                // ...
	            	System.out.println(response);
	            } else {
	                // 处理错误响应
	                // ...
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	}
